<?php 
class Wmp_File
{
	//TODO : faire une possibilité de prévisualiser des fichiers
	//pdf, images, swf, flv. Faire un test sur l'extension
	//et appeler la commande adequate
	static function previewFile($file)
	{
		
	}
	
	static function getDirectories($path)
	{
		if ($handle = opendir($path)) {   
    		$files = array();			
    		while (false !== ($file = readdir($handle))) 
        		if ( $file != "." && $file != ".." ) 
            		$files[]=$file;
           
    		closedir($handle);
    		
    		return $files; 
		}
		return false;		
	}
		
	static function copyDirectory( $source, $target )
    {
        if ( is_dir( $source ) )  {
            @mkdir( $target );
           
            $d = dir( $source );
           
            while ( FALSE !== ( $entry = $d->read() ) )
            {
                if ( $entry == '.' || $entry == '..' )  {
                    continue;
                }
               
                $Entry = $source . '/' . $entry;           
                if ( is_dir( $Entry ) )  {
                    Wmp_File::copyDirectory( $Entry, $target . '/' . $entry );
                    continue;
                }
                copy( $Entry, $target . '/' . $entry );
            }
           
            $d->close();
        }else
        {
            copy( $source, $target );
        }
    }
	
	static function deleteDirectory($target)
	{
		$exceptions=array('.','..');
		if (!$sourcedir=@opendir($target))
		    return false;
		    
		while(false!==($sibling=readdir($sourcedir))) {
		    if(!in_array($sibling,$exceptions)) {
		        $object=str_replace('//','/',$target.'/'.$sibling);
		        
		        if(is_dir($object))
		            Wmp_File::deleteDirectory($object);
		        if(is_file($object))
		            $result=@unlink($object);
		    }
		}
		closedir($sourcedir);
		
		if($result=@rmdir($target))
		      return true;
		    
		return false;
	}
	
	static function getFiles($path)
	{
		if ($handle = opendir($path)) {   
    		$files = array();			
    		while (false !== ($file = readdir($handle))){ 
        		if(strpos($file, "Controller.php")!==false) {
        				$files[]=$file;
        		}
    		}
    		closedir($handle);
		}	
		return $files;
	}
}